//
//  HDOrderView.m
//  HDecoration
//
//  Created by 曹群 on 15/8/16.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDOrderView.h"
#import <CoreLocation/CoreLocation.h>
@interface HDOrderView ()
@property(nonatomic,strong)UITableView *contentTableView;
@property(nonatomic,strong)DOImageView *photo;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *stationTimeLabel;
@property(nonatomic,strong)UILabel *typeLabel;
@property(nonatomic,strong)UILabel *learnTimeLabel;
@property(nonatomic,strong)UILabel *remarkLabel;
@property(nonatomic,strong)GXButton *locationBtn;
@property(nonatomic,strong)UILabel *subjectLabel;
@property(nonatomic,strong)UILabel *distanceLabel;
@property(nonatomic,strong)GXButton *needCompanyBtn;
@property(nonatomic,strong)UILabel *priceLabel;
@property(nonatomic,strong)UIButton *orderTypeBtn;
@property(nonatomic,strong)UIButton *acceptBtn;
@end

@implementation HDOrderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds = YES;
        self.hidden = YES;
        [self addSubview:self.contentTableView];
    }
    return self;
}

- (void)showWithData:(HDCustomerInfo *)data
{
    self.hidden = YES;
    if (([netEngine.coachInfo.user_type intValue]==1&&data.orderType==HDOrderTypePractice)||([netEngine.coachInfo.user_type intValue]==2&&data.orderType==HDOrderTypeLearn))
    {
        self.acceptBtn.hidden = YES;
    }
    else
    {
        self.acceptBtn.hidden = NO;
    }
    self.photo.imgURLStr = data.photoUrl;
    self.nameLabel.text = data.name;
    self.stationTimeLabel.text = [NSString stringWithFormat:@"平台学时：%@",data.stationTime];
    self.typeLabel.text = [NSString stringWithFormat:@"驾考类型：%@",data.type];
    self.subjectLabel.text = data.subject;
    if (data.orderType==HDOrderTypePractice)
    {
        self.remarkLabel.text = [NSString stringWithFormat:@"备注：%@",data.remark];
        [self.orderTypeBtn setBackgroundImage:[UIImage imageNamed:@"练车订单"] forState:UIControlStateNormal];
        [self.orderTypeBtn setTitle:@"练车订单" forState:UIControlStateNormal];
    }
    else
    {
        [self.orderTypeBtn setBackgroundImage:[UIImage imageNamed:@"学车订单"] forState:UIControlStateNormal];
        [self.orderTypeBtn setTitle:@"学车订单" forState:UIControlStateNormal];
        self.remarkLabel.text = [NSString stringWithFormat:@"训练场地：%@",data.practicePosition];
    }
    if (!_separateOrderType)
    {
        self.subjectLabel.left = self.orderTypeBtn.left-self.subjectLabel.width-10;
    }
    else
    {
        [self.orderTypeBtn removeFromSuperview];
        self.subjectLabel.left = APPFRAME.size.width-60;
    }
    if (data.startTime!=nil&&data.endTime!=nil)
    {
        NSString *date = [HDUtil formatter:@"M月d日" FromTime:[data.startTime doubleValue]];
        NSString *startTime = [HDUtil formatter:@"HH:mm" FromTime:[data.startTime doubleValue]];
        NSString *endTime = [HDUtil formatter:@"HH:mm" FromTime:[data.endTime doubleValue]];
        self.learnTimeLabel.text = [NSString stringWithFormat:@"学车时间：%@ %@-%@ %@个小时",date,startTime,endTime,[HDUtil check:data.learnTime]];
    }
    self.priceLabel.text = [NSString stringWithFormat:@"%@元",data.price];
    [self.locationBtn setImage:[UIImage imageNamed:@"定位"] forState:UIControlStateNormal];
    [self.locationBtn setTitle:data.location forState:UIControlStateNormal];
    self.needCompanyBtn.hidden = !data.needCompany;
    
    if (netEngine.coachInfo.xy.length && data.xy.length)
    {
        NSString* xy=netEngine.coachInfo.xy;
        xy=[xy stringByReplacingOccurrencesOfString:@"(" withString:@""];
        xy=[xy stringByReplacingOccurrencesOfString:@")" withString:@""];
        NSArray *array=[xy componentsSeparatedByString:@","];
        CLLocation *coachLocation=[[CLLocation alloc] initWithLatitude:[array[0] floatValue] longitude:[array[1] floatValue]];
        CLLocation *userLocation=[[CLLocation alloc] initWithLatitude:data.latitude longitude:data.longitude];
        // 计算距离
        CLLocationDistance meters=[coachLocation distanceFromLocation:userLocation];
        self.distanceLabel.text = [NSString stringWithFormat:@"距离我%.1fKM",meters/1000.0];
    }

    self.hidden = NO;
    self.contentTableView.top = self.contentTableView.top+self.contentTableView.height;
    [UIView animateWithDuration:0.3 animations:^{
        self.contentTableView.top = self.contentTableView.top-self.contentTableView.height;
    }];
}

- (void)dismiss
{
    self.hidden = YES;
}

- (void)acceptOrder:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(orderViewAcceptOrder)]) {
        [self.delegate orderViewAcceptOrder];
    }
}

#pragma mark - UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row==0) {
            [cell.contentView addSubview:self.photo];
            [cell.contentView addSubview:self.nameLabel];
            [cell.contentView addSubview:self.stationTimeLabel];
            [cell.contentView addSubview:self.typeLabel];
            [cell.contentView addSubview:self.subjectLabel];
            if (!_separateOrderType) {
                [cell.contentView addSubview:self.orderTypeBtn];
            }
        } else if (indexPath.row==1) {
            UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 0.5)];
            line.image = [UIImage imageNamed:@"dot"];
            [cell.contentView addSubview:line];
            [cell.contentView addSubview:self.learnTimeLabel];
            [cell.contentView addSubview:self.remarkLabel];
            [cell.contentView addSubview:self.priceLabel];
        } else if (indexPath.row==2) {
            UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 0.5)];
            line.image = [UIImage imageNamed:@"dot"];
            [cell.contentView addSubview:line];
            [cell.contentView addSubview:self.locationBtn];
            [cell.contentView addSubview:self.distanceLabel];
            [cell.contentView addSubview:self.needCompanyBtn];
        }
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 200)];
    [footerView addSubview:self.acceptBtn];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        return 80;
    } else if (indexPath.row==1) {
        return 70;
    } else if (indexPath.row==2) {
        return 60;
    } else {
        return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 200;
}

#pragma mark - getters
- (UITableView *)contentTableView
{
    if (!_contentTableView) {
        _contentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) style:UITableViewStyleGrouped];
        _contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _contentTableView.delegate = (id)self;
        _contentTableView.dataSource = (id)self;
        _contentTableView.backgroundColor = [UIColor whiteColor];
        _contentTableView.bounces = NO;
        UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 0.5)];
        topLine.image = [UIImage imageNamed:@"dot"];
        [_contentTableView addSubview:topLine];
        UIImageView *bottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.height-0.5, APPFRAME.size.width, 0.5)];
        bottomLine.image = [UIImage imageNamed:@"dot"];
        [_contentTableView addSubview:bottomLine];
    }
    return _contentTableView;
}

- (DOImageView *)photo
{
    if (!_photo) {
        _photo = [[DOImageView alloc] initWithFrame:CGRectMake(10, 15, 50, 50)];
        _photo.layer.cornerRadius = 5;
        _photo.layer.masksToBounds = YES;
        _photo.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _photo;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, 15, 100, 20)];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textColor = kDarkGrayColor;
    }
    return _nameLabel;
}

- (UILabel *)stationTimeLabel
{
    if (!_stationTimeLabel) {
        _stationTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, self.nameLabel.bottom, APPFRAME.size.width-130, 15)];
        _stationTimeLabel.font = [UIFont systemFontOfSize:12];
        _stationTimeLabel.textColor = [UIColor hexStringToColor:@"#424242"];
    }
    return _stationTimeLabel;
}

- (UILabel *)typeLabel
{
    if (!_typeLabel) {
        _typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, self.stationTimeLabel.bottom, APPFRAME.size.width-130, 15)];
        _typeLabel.font = [UIFont systemFontOfSize:12];
        _typeLabel.textColor = kLightGrayColor;
    }
    return _typeLabel;
}

- (UILabel *)learnTimeLabel
{
    if (!_learnTimeLabel) {
        _learnTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, APPFRAME.size.width-20, 15)];
        _learnTimeLabel.font = [UIFont systemFontOfSize:13];
        _learnTimeLabel.textColor = kDarkGrayColor;
    }
    return _learnTimeLabel;
}

- (UILabel *)remarkLabel
{
    if (!_remarkLabel) {
        _remarkLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.learnTimeLabel.bottom+10, APPFRAME.size.width-95, 15)];
        _remarkLabel.font = [UIFont systemFontOfSize:13];
        _remarkLabel.textColor = kDarkGrayColor;
    }
    return _remarkLabel;
}

- (GXButton *)locationBtn
{
    if (!_locationBtn) {
        _locationBtn = [GXButton buttonWithType:UIButtonTypeCustom];
        _locationBtn.frame = CGRectMake(10, 15, APPFRAME.size.width-20, 20);
        _locationBtn.imageViewFrame = CGRectMake(5, 3, 9, 13);
        _locationBtn.titleLabelFrame = CGRectMake(23, 0, _locationBtn.width-19, 20);
        [_locationBtn setTitleColor:kDarkGrayColor forState:UIControlStateNormal];
        _locationBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        _locationBtn.relayout = YES;
        _locationBtn.userInteractionEnabled = NO;
    }
    return _locationBtn;
}

- (UILabel *)subjectLabel
{
    if (!_subjectLabel) {
        _subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(APPFRAME.size.width-60, 35, 50, 20)];
        _subjectLabel.font = [UIFont systemFontOfSize:13];
        _subjectLabel.textColor = kDarkGrayColor;
    }
    return _subjectLabel;
}

- (UILabel *)distanceLabel
{
    if (!_distanceLabel) {
        _distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.locationBtn.bottom, APPFRAME.size.width-100, 20)];
        _distanceLabel.textColor = kDarkGrayColor;
        _distanceLabel.font = [UIFont systemFontOfSize:13];
    }
    return _distanceLabel;
}

- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(APPFRAME.size.width-70, self.learnTimeLabel.bottom+5, 60, 30)];
        _priceLabel.textColor = [UIColor hexStringToColor:@"#ff2b01"];
        _priceLabel.font = [UIFont systemFontOfSize:13];
    }
    return _priceLabel;
}

- (GXButton *)needCompanyBtn
{
    if (!_needCompanyBtn)
    {
        _needCompanyBtn = [GXButton buttonWithType:UIButtonTypeCustom];
        _needCompanyBtn.frame = CGRectMake(APPFRAME.size.width-83, self.locationBtn.bottom, 73, 25);
        _needCompanyBtn.imageViewFrame = CGRectMake(2, 5, 19, 14);
        _needCompanyBtn.titleLabelFrame = CGRectMake(23, 0, 50, 25);
        _needCompanyBtn.relayout = YES;
        [_needCompanyBtn setTitle:@"需要接送" forState:UIControlStateNormal];
        [_needCompanyBtn setImage:[UIImage imageNamed:@"汽车.jpg"] forState:UIControlStateNormal];
        [_needCompanyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _needCompanyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        _needCompanyBtn.userInteractionEnabled = NO;
        _needCompanyBtn.hidden = YES;
    }
    return _needCompanyBtn;
}

- (UIButton *)orderTypeBtn
{
    if (!_orderTypeBtn) {
        _orderTypeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _orderTypeBtn.frame = CGRectMake(self.width-70, 30, 60, 30);
        [_orderTypeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _orderTypeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        _orderTypeBtn.userInteractionEnabled = NO;
    }
    return _orderTypeBtn;
}

- (UIButton *)acceptBtn
{
    if (!_acceptBtn) {
        _acceptBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _acceptBtn.frame = CGRectMake(self.width/2.0-50, 30, 100, 100);
        _acceptBtn.layer.cornerRadius = 50;
        [_acceptBtn setBackgroundImage:[UIImage imageNamed:@"接"] forState:UIControlStateNormal];
        [_acceptBtn setTitle:@"接" forState:UIControlStateNormal];
        [_acceptBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _acceptBtn.titleLabel.font = [UIFont systemFontOfSize:30];
        [_acceptBtn addTarget:self action:@selector(acceptOrder:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _acceptBtn;
}

@end
